/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lcfgly.View;

import com.lcfgly.Database.ShopPrices;
import com.lcfgly.Database.UserInfo;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Interface.UserSession;
import com.lcfgly.Service.PriceService;
import com.lcfgly.Service.ProductService;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author lcfgly
 */
@Controller
@RequestMapping("/control")
public class PriceControl extends FatherController {

    @Autowired
    ProductService ProductService;

    @Autowired
    PriceService PriceService;

    /**
     * 产品列表页
     *
     * @param type
     * @return
     * @throws ActionException
     */
    @RequestMapping(value = "/product/{type}", method = RequestMethod.GET)
    public ModelAndView product(@PathVariable("type") int type) throws ActionException {
        ModelAndView result = new ModelAndView("controller/product", "type", ProductService.GetTypeInfo(type));
        result.addObject("prodcutarctype", true);
        return result;
    }

    /**
     * 产品列表页
     *
     * @return
     * @throws ActionException
     */
    @RequestMapping(value = "/productall", method = RequestMethod.GET)
    public ModelAndView productall() throws ActionException {
        ModelAndView result = new ModelAndView("controller/productall");
        result.addObject("prodcutarctype", true);
        return result;
    }

    /**
     * 所有产品JSON数据
     *
     * @param session
     * @param request
     * @return
     * @throws ActionException
     */
    @RequestMapping(value = "/productlist", method = RequestMethod.POST)
    @ResponseBody
    public Map ProductList(@UserSession UserInfo session, HttpServletRequest request) throws ActionException {
        Map result = ProductService.GetProductListForDataTable(request, session.getUid());
        List<Map> res = (List) result.get("aaData");
        for (Map rs : res) {
            if (rs.get("price") == null) {
                rs.put("price", "");
            }
            if(rs.get("litpic") != null){
                String[] litpic = ((String) rs.get("litpic")).split("\\.");
                if (litpic.length == 2 && litpic[0].endsWith("_thumb")) {
                    rs.put("litpic", litpic[0].substring(0, litpic[0].length() - 6) + "." + litpic[1]);
                }
            }
        }
        return result;
    }

    /**
     * 产品JSON数据
     *
     * @param session
     * @param request
     * @param typeid
     * @return
     * @throws ActionException
     */
    @RequestMapping(value = "/productlist/{typeid}", method = RequestMethod.POST)
    @ResponseBody
    public Map ProductList(@UserSession UserInfo session, HttpServletRequest request, @PathVariable("typeid") int typeid) throws ActionException {
        Map result = ProductService.GetProductListForDataTable(request, typeid, session.getUid());
        List<Map> res = (List) result.get("aaData");
        for (Map rs : res) {
            if (rs.get("price") == null) {
                rs.put("price", "");
            }
            if(rs.get("litpic") != null){
                String[] litpic = ((String) rs.get("litpic")).split("\\.");
                if (litpic.length == 2 && litpic[0].endsWith("_thumb")) {
                    rs.put("litpic", litpic[0].substring(0, litpic[0].length() - 6) + "." + litpic[1]);
                }
            }
        }
        return result;
    }

    /**
     * 获取产品信息
     *
     * @param typeid
     * @param id
     * @param session
     * @return
     * @throws ActionException
     */
    @RequestMapping(value = "/productinfo/{type}/{id}", method = RequestMethod.GET)
    public ModelAndView ProductInfo(@PathVariable("type") int typeid, @PathVariable("id") int id, @UserSession UserInfo session) throws ActionException {
        Map result = new HashMap();
        result.put("type", ProductService.GetTypeInfo(typeid));
        result.put("product", ProductService.GetProductInfo(typeid, id));
        String addtable = (String) ((Map) result.get("product")).get("addtable");
        result.put("productproperty", ProductService.GetProductProperty(addtable));
        result.put("productimgs", ProductService.GetProductImgs(id, addtable));
        try {
            result.put("price", PriceService.find(session.getUid(), id));
        } catch (ActionException e) {
            //未查找到价格
        }
        return new ModelAndView("controller/productshow", result);
    }

    /**
     * 设置价格
     *
     * @param request
     * @param typeid
     * @param id
     * @param session
     * @return
     * @throws ActionException
     */
    @RequestMapping(value = "/pricepost/{typeid}/{id}", method = RequestMethod.POST)
    @ResponseBody
    public boolean PricePost(HttpServletRequest request, @PathVariable("typeid") short typeid, @PathVariable("id") int id, @UserSession UserInfo session) throws ActionException {
        float x = Float.valueOf(request.getParameter("price"));
        ShopPrices price = new ShopPrices();
        price.setProduct(id);
        price.setPrice(x);
        price.setShopid(session.getUid());
        price.setProducttype(typeid);
        price.setUpdatetime(new Date(new Date().getTime()));
        price.setPriceimg(request.getParameter("priceimg"));
        if(PriceService.create(price, session.getUid())){
            HttpSession ses=request.getSession(true);
            Integer xx=(Integer)ses.getAttribute("shopperfect");
            if(xx!=null && xx>1)
                ses.setAttribute("shopperfect", --xx);
            else
                ses.setAttribute("shopperfect",0);
            return true;
        } else
            return false;
    }

}
